package com.tengroup.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tengroup.dao.QuestionMapper;
import com.tengroup.entity.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

    private final QuestionMapper questionMapper;

    public QuestionServiceImpl(QuestionMapper questionMapper) {
        this.questionMapper = questionMapper;
    }

    // 增加题目
    @Override
    public boolean addQuestion(Question question) {
        return this.save(question);
    }

    // 删除题目（软删除）
    @Override
    public boolean deleteQuestion(Long id) {
        Question question = this.getById(id);
        if (question != null) {
            question.setDelFlag(true);
            return this.updateById(question);
        }
        return false;
    }

    // 修改题目
    @Override
    public boolean updateQuestion(Question question) {
        return this.updateById(question);
    }

    // 根据 ID 查询题目
    @Override
    public Question getQuestionById(Long id) {
        return this.getById(id);
    }

    // 查询所有未删除的题目
    @Override
    public List<Question> getAllQuestions() {
        return questionMapper.findAllQuestions();
    }

    // 分页查询所有未删除的题目
    @Override
    public IPage<Question> getAllQuestions(int page, int size) {
        Page<Question> pageParam = new Page<>(page, size);
        return questionMapper.findAllQuestions(pageParam);
    }

    // 根据题目类型查询题目列表
    @Override
    public List<Question> findByQuestionType(Integer questionType) {
        return questionMapper.findByQuestionType(questionType);
    }

    // 根据科目 ID 查询题目列表
    @Override
    public List<Question> findBySubjectId(Integer subjectId) {
        return questionMapper.findBySubjectId(subjectId);
    }

    // 根据难度级别查询题目列表
    @Override
    public List<Question> findByDifficulty(Byte difficulty) {
        return questionMapper.findByDifficulty(difficulty);
    }

    // 根据关键字模糊查询题目列表
    @Override
    public List<Question> findByKeyword(String keyword) {
        return questionMapper.findByKeyword(keyword);
    }


    // 查询所有已删除的题目
    @Override
    public List<Question> findAllDeletedQuestions() {
        return questionMapper.findAllDeletedQuestions();
    }

    // 批量导入题目
    @Override
    public boolean importQuestions(List<Question> questions) {
        if (questions == null || questions.isEmpty()) {
            return false;
        }
        int result = questionMapper.insertBatch(questions);
        return result > 0;
    }
    @Override
    public void removeDuplicateQuestions() {
        // 1. 查找所有重复的题目 stem
        List<String> duplicateStems = questionMapper.findDuplicateStems();

        // 2. 遍历每个重复的 stem，删除重复项
        for (String stem : duplicateStems) {
            // 查找需要保留的最小 id
            QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("MIN(id) AS id")
                    .eq("stem", stem)
                    .eq("del_flag", 0)
                    .groupBy("stem");
            List<Object> minIds = questionMapper.selectObjs(queryWrapper);

            if (!minIds.isEmpty()) {
                Long minId = (Long) minIds.get(0);

                // 删除除最小 id 以外的记录
                QueryWrapper<Question> deleteWrapper = new QueryWrapper<>();
                deleteWrapper.eq("stem", stem)
                        .ne("id", minId)
                        .eq("del_flag", 0);
                questionMapper.delete(deleteWrapper);
            }
        }
    }

}